﻿using OnlineAuction.Business;
using OnlineAuction.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace OnlineAuction.Users
{
    public partial class AddItems : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Cookies["UserInfo"] == null)
            {
                Response.Redirect("../Home.aspx");
            }
            else if (!Page.IsPostBack)
            {
                //Get all category
                CategoryBO categoryBO = new CategoryBO();
                ddlCategoryName.DataTextField = "CategoryName";
                ddlCategoryName.DataValueField = "CategoryID";
                ddlCategoryName.DataSource = categoryBO.GetCategory();
                ddlCategoryName.DataBind();
                //Get all auction type
                AuctionTypeBO auctionTypeBO = new AuctionTypeBO();
                rbtnAuctionTypeName.DataTextField = "AuctionTypeName";
                rbtnAuctionTypeName.DataValueField = "AuctionTypeID";
                rbtnAuctionTypeName.DataSource = auctionTypeBO.GetAuctionType();
                rbtnAuctionTypeName.DataBind();
                rbtnAuctionTypeName.SelectedIndex = 0;
            }
            //Reload input elements 
            if (rbtnAuctionTypeName.SelectedItem.ToString().CompareTo("auto") == 0)
            {
                txtExpectationPrice.Enabled = false;
                rfvExpectationPrice.Enabled = false;
                txtBidIncrement.Enabled = true;
                rfvBidIncrement.Enabled = true;
            }
            else
            {
                txtBidIncrement.Enabled = false;
                rfvBidIncrement.Enabled = false;
                txtExpectationPrice.Enabled = true;
                rfvExpectationPrice.Enabled = true;
            }
        }
        //Reset all information
        protected void btnReset_Click(object sender, EventArgs e)
        {
            txtItemName.Text = "";
            fuItemImage1.FileName.Remove(0);
            fuItemImage2.FileName.Remove(0);
            fuItemImage3.FileName.Remove(0);
            fuItemImage4.FileName.Remove(0);
            txtItemDescription.Text = "";
            txtFirstPrice.Text = "";
            txtExpectationPrice.Text = "";
            txtTimeFinishAuction.Text = "";
            txtBidIncrement.Text = "";
            txtTimeIncrement.Text = "";
        }
        //Add new item
        protected void btnAddItem_Click(object sender, EventArgs e)
        {
            ItemBO bo = new ItemBO();
            string userName = Request.Cookies["UserInfo"]["UserName"].ToString();
            //Save as image to server's folder
            string path = Server.MapPath("~/Image1/");
            fuItemImage1.SaveAs(path + fuItemImage1.FileName);
            if (fuItemImage2.HasFile == true)
            {
                fuItemImage2.SaveAs(path + fuItemImage2.FileName);
            }
            if (fuItemImage3.HasFile == true)
            {
                fuItemImage3.SaveAs(path + fuItemImage3.FileName);
            }
            if (fuItemImage4.HasFile == true)
            {
                fuItemImage4.SaveAs(path + fuItemImage4.FileName);
            }
            //create new item entity
            ItemsE item = new ItemsE(int.Parse(ddlCategoryName.SelectedValue), txtItemName.Text,
                    "Image1/" + fuItemImage1.FileName, fuItemImage2.HasFile == true ? "Image1/" + fuItemImage2.FileName : "",
                    fuItemImage3.HasFile == true ? "Image1/" + fuItemImage3.FileName : "",
                    fuItemImage4.HasFile == true ? "Image1/" + fuItemImage4.FileName : "",
                    txtItemDescription.Text);
            //create new auction entity
            AuctionDTO auction = new AuctionDTO(int.Parse(rbtnAuctionTypeName.SelectedValue),
                    double.Parse(txtFirstPrice.Text),
                    double.Parse(txtExpectationPrice.Text.CompareTo("") == 0 ? "0" : txtExpectationPrice.Text),
                    txtTimeFinishAuction.Text,
                    double.Parse(txtBidIncrement.Text.CompareTo("") == 0 ? "0" : txtBidIncrement.Text),
                    int.Parse(txtTimeIncrement.Text), userName);
            //Check result add item
            bool flag = bo.AddItems(item, auction);
            if (flag)
            {
                Response.Redirect("ManageHome.aspx");
            }
            else
            {
                Response.Write("<script>alert('Error');</script>");
            }
        }
    }
}